package com.dhcc.logs.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dhcc.logs.common.annotation.Log;
import com.dhcc.logs.common.annotation.MyLog;
import com.dhcc.logs.common.enums.BusinessType;
import com.dhcc.logs.common.response.Result;
import com.dhcc.logs.common.response.ResultCode;
import com.dhcc.logs.entity.User;
import com.dhcc.logs.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/demo")
@Api(tags = "LogDemo案例")
public class DemoController{
	private static final Logger log = LoggerFactory.getLogger(DemoController.class);
	
	@Autowired
	UserService userService;
	
    @MyLog(value = "获取数据")  //这里添加了AOP的自定义注解
    @RequestMapping(value = "/getData/{msg}",method = RequestMethod.GET)
    public Result getData(@PathVariable String msg) {
        return new Result(ResultCode.SUCCESS,"获取数据"+msg);
    }
    @RequestMapping(value = "/getDataNolog/{msg}",method = RequestMethod.GET)
    public Result getDataNolog(@PathVariable String msg) {
    	return new Result(ResultCode.SUCCESS,"获取数据"+msg);
    }
    @ApiOperation(value = "添加用户")
    @Log(title = "添加用户", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add/{name}",method = RequestMethod.POST)
    public Result add(@ApiParam(value = "用户名") @PathVariable(name = "name") String name){
    	log.info("添加用户：" + name);
    	User user = new User();
    	user.setName(name);
    	userService.save(user);
        return new Result(ResultCode.SUCCESS,"添加用户：" + name);
    }
    @ApiOperation(value = "删除用户")
    @Log(title = "删除用户", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public Result delete(@ApiParam(value = "id") @PathVariable(name = "id",required = true) String id){
        log.info("删除用户：" + id );
        userService.deleteById(id);
        return new Result(ResultCode.SUCCESS,"删除用户：" + id);
    }
    @ApiOperation(value = "修改用户")
    @Log(title = "修改用户", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT)
    public Result update(@ApiParam(value = "用户id") @PathVariable(name = "id") String id){
        log.info("修改用户：" + id);
        return new Result(ResultCode.SUCCESS,"修改用户：" + id);
    }
    @ApiOperation(value = "查询用户")
    @Log(title = "查询用户", businessType = BusinessType.FIND)
    @RequestMapping(value = "/query/{name}",method = RequestMethod.GET)
    public Result query(@ApiParam(value = "用户名") @PathVariable(name = "name") String name){
        log.info("查询用户：" + name);
        return new Result(ResultCode.SUCCESS,"查询用户：" + name);
    }
    @ApiOperation(value = "验证异常")
    @Log(title = "验证异常", businessType = BusinessType.VERIFY)
    @RequestMapping(value = "/verifyThrow/{name}/{num}",method = RequestMethod.GET)
    public Result verifyThrow(@ApiParam(value = "用户名") @PathVariable(name = "name",required =false) String name,
    		@ApiParam(value = "num",example = "1") @PathVariable(name = "num") Integer num){
    	//Swagger2异常:java.lang.NumberFormatException:For input string:""解决@ApiParam添加example属性，并且值必须为数字类型。
    	//https://blog.csdn.net/lilyssh/article/details/82944507
    	log.info("验证异常：" + name);
    	return new Result(ResultCode.SUCCESS,"验证异常：" + name+"运算："+(10/num));
    }
    
}